package com.example.loginstructuredemo.model;

import com.example.loginstructuredemo.base.BaseModel;
import com.example.loginstructuredemo.base.IBaseRequestCallBack;
import com.example.loginstructuredemo.entity.bean.ArtUserBo;
import com.example.loginstructuredemo.entity.bean.HttpResult;
import com.example.loginstructuredemo.http.HttpMethodsRetrofit;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Alex on 2016/10/17.
 */

public class LoginBus extends BaseModel{

//	LoginService loginService;

	private static final String TAG = "LoginBus";

	public void login(String name, String password, final IBaseRequestCallBack baseRequestCallBack, final int requestTag) {
//		loginService = HttpMethodsRetrofit.getRetrofit().create(LoginService.class);
//		Observable observable = loginService.login(name, password)
//				.map(new HttpResultFunc());
//		observable
//				.unsubscribeOn(Schedulers.io())
//				.subscribeOn(Schedulers.io())
//				.observeOn(AndroidSchedulers.mainThread())
//				.subscribe(new Observer<ArtUserBo>(){
//
//					@Override
//					public void onCompleted() {
//
//					}
//
//					@Override
//					public void onError(Throwable e) {
//						loadLoginCallCallback.onLoginFailed();
//					}
//
//					@Override
//					public void onNext(ArtUserBo artUserBo) {
////						ArtUserBo artUserBo = (ArtUserBo) artUserBoHttpResult.getResult();
//						loadLoginCallCallback.onLogin(artUserBo);
//					}
//				});

		HttpMethodsRetrofit.getRetrofit().create(LoginService.class)
				.login(name, password)
				.unsubscribeOn(Schedulers.io())
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<HttpResult<ArtUserBo>>() {
					@Override
					public void onCompleted() {

					}

					@Override
					public void onError(Throwable e) {
						baseRequestCallBack.requestError(e,requestTag);
					}

					@Override
					public void onNext(HttpResult<ArtUserBo> artUserBoHttpResult) {
						ArtUserBo artUserBo = artUserBoHttpResult.getResult();
						baseRequestCallBack.retuestSuccess(artUserBo,requestTag);
					}
				});
//		toSubscribe(observable, subscriber);
	}

//	private class HttpResultFunc<T> implements Func1<HttpResult<ArtUserBo>, ArtUserBo> {
//		@Override
//		public ArtUserBo call(HttpResult httpResult) {
////			Gson gson = new Gson();
//			String json;
//			ArtUserBo artUserBo = null;
////			if (httpResult != null) {
////				json = httpResult.toString();
//////				artUserBo = gson.fromJson(json, ArtUserBo.class);
////			}
//			return artUserBo;
//		}
//	}

//	private class HttpResultFunc<ArtuserBo> implements Func1<HttpResult<ArtuserBo>, ArtuserBo>{
//
//		@Override
//		public ArtuserBo call(HttpResult<ArtuserBo> httpResult) {
//			if (httpResult != null) {
//				Log.d(TAG, "call() called with: httpResult = [" + httpResult + "]");
//			}
//			return httpResult.getResult();
//		}
//	}
}
